package pl.edu.pw.polygen.menu.commands;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.vaadin.dialogs.ConfirmDialog;

import pl.edu.pw.polygen.util.ApplicationHelper;
import pl.edu.pw.polygen.util.ApplicationHolder;

import com.vaadin.terminal.ExternalResource;
import com.vaadin.ui.MenuBar.Command;
import com.vaadin.ui.MenuBar.MenuItem;

@Component
public class LogoutCommand implements Command{

	private static final long serialVersionUID = 3036720725812223037L;

	private static final Logger LOG = Logger.getLogger(LogoutCommand.class);
	
	@Override
	public void menuSelected(MenuItem selectedItem) {
		try{
			ConfirmDialog.show(ApplicationHelper.getCurrentApplication().getMainWindow(),"Logout confirm", "Do you really want to logout?","Logout","Cancel",
			        new ConfirmDialog.Listener() {
			            public void onClose(ConfirmDialog dialog) {
			                if (dialog.isConfirmed()) {
			                	String context = ApplicationHolder.getApplication().getURL().getPath();
			        			ApplicationHolder.getApplication().getMainWindow().open(new ExternalResource(context + "j_spring_security_logout"));
			                } 
			            }
			        });
		}
		catch(Exception e){
			LOG.error(e.getMessage());
		}
	}

}
